define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'tongcheng/rider/index' + location.search,
                    add_url: 'tongcheng/rider/add',
                    edit_url: 'tongcheng/rider/edit',
                    del_url: 'tongcheng/rider/del',
                    multi_url: 'tongcheng/rider/multi',
                    import_url: 'tongcheng/rider/import',
                    table: 'tongcheng_rider',
                }
            });

            var table = $("#table");

            // 在普通搜索渲染后
            table.on('post-common-search.bs.table', function (event, table) {
                let form = $("form", table.$commonsearch);

                $("input[name='zone_id']", form).addClass("selectpage").data("source", "tongcheng/zone/index").data("params", {"custom[status]": 1}).data("orderBy","id desc");
                $("input[name='user_id']", form).addClass("selectpage").data("source", "user/user/index").data("field", "nickname").data("orderBy","id desc");

                // 追加只显示在线状态复选框
                /*var online_status_html = '<div class="form-group col-xs-12 col-sm-6 col-md-4 col-lg-3">';
                online_status_html += '<label class="control-label col-xs-4">在线状态</label>';
                online_status_html += '<div class="col-xs-8">';
                online_status_html += '<select class="form-control" name="online_status">';
                online_status_html += '<option value="">全部</option>';
                online_status_html += '<option value="1">在线</option>';
                // online_status_html += '<option value="0">离线</option>';
                online_status_html += '</select>';
                online_status_html += '</div>';
                online_status_html += '</div>';
                $('.form-commonsearch >fieldset >.row').children().last().before(online_status_html);*/

                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'zone_id', title: __('Zone_id'), visible: false,},
                        // {
                        //     field: 'zone.name', title: __('Zone.name'), operate: false, formatter: function (value, row, index) {
                        //         return value + '(' + row.zone_id + ')';
                        //     }
                        // },
                        {field: 'user_id', title: __('User_id'), visible: false},
                        // {
                        //     field: 'user.nickname', title: __('User.nickname'), operate: false, formatter: function (value, row, index) {
                        //         return value + '(' + row.user_id + ')';
                        //     }
                        // },

                        // {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'real_name', title: __('Real_name'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        // {field: 'gender', title: __('Gender'), searchList: {"1":__('Gender 1'),"0":__('Gender 0')}, formatter: Table.api.formatter.normal},
                        // {field: 'id_card_no', title: __('Id_card_no'), operate: 'LIKE'},
                        {field: 'lng', title: __('Lng'), operate: false},
                        {field: 'lat', title: __('Lat'), operate: false},
                        // {field: 'online_status', title: __('online_status'), searchList: {"1":__('online_status 1'),"0":__('online_status 0')}, formatter: Table.api.formatter.status},
                        {field: 'work_type', title: __('Work_type'), searchList: {"full":__('Work_type full'),"part":__('Work_type part')}, formatter: Table.api.formatter.status},
                        {field: 'work_status', title: __('Work_status'), searchList: {"1":__('Work_status 1'),"0":__('Work_status 0')}, formatter: Table.api.formatter.status},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"0":__('Status 0')}, formatter: Table.api.formatter.status},
                        {
                            field: 'day_first_take_deduct_fee', title: __('day_first_take_deduct_fee'), operate: false, formatter: function (value, row, index) {
                                if (value == 0) {
                                    return '-';
                                }
                                return value + '元';
                            }
                        },
                        {
                            field: 'day_order_limit', title: __('Day_order_limit'), operate: false, formatter: function (value, row, index) {
                                if (value == 0) {
                                    return '不限';
                                }
                                return value + '单';
                            }
                        },
                        {
                            field: 'taking_order_limit', title: __('Taking_order_limit'), operate: false, formatter: function (value, row, index) {
                                if (value == 0) {
                                    return '不限';
                                }
                                return value + '单';
                            }
                        },
                        {
                            field: 'today_order_count', title: __('Today_order_count'), operate: false, formatter: function (value, row, index) {
                                if (value == 0) {
                                    return '不限';
                                }
                                return value + '单';
                            }
                        },
                        {
                            field: 'taking_order_count', title: __('Taking_order_count'), operate: false, formatter: function (value, row, index) {
                                if (value == 0) {
                                    return '不限';
                                }
                                return value + '单';
                            }
                        },
                        {
                            field: 'zone_delivery_fee_rate', title: __('Zone_delivery_fee_rate'), operate: false,
                            formatter: function (value, row, index) {
                                return (value) + '%';// * 100
                            }
                        },
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            $(document).on('click', '.btn-multi-set', function () {
              let ids = Table.api.selectedids(table);
              console.log('ids', ids)
              Fast.api.open('tongcheng/rider/multiset?ids=' + ids.join(','), '批量设置', {ids: ids.join(',')})
            });
        },
        online: function () {
          // 初始化表格参数配置
          Table.api.init({
            extend: {
              index_url: 'tongcheng/rider/online' + location.search,
              // add_url: 'tongcheng/rider/add',
              // edit_url: 'tongcheng/rider/edit',
              // del_url: 'tongcheng/rider/del',
              // multi_url: 'tongcheng/rider/multi',
              // import_url: 'tongcheng/rider/import',
              table: 'tongcheng_rider',
            }
          });

          var table = $("#table");

          // 在普通搜索渲染后
          table.on('post-common-search.bs.table', function (event, table) {
            let form = $("form", table.$commonsearch);

            $("input[name='zone_id']", form).addClass("selectpage").data("source", "tongcheng/zone/index").data("params", {"custom[status]": 1}).data("orderBy","id desc");
            $("input[name='user_id']", form).addClass("selectpage").data("source", "user/user/index").data("field", "nickname").data("orderBy","id desc");

            // 追加只显示在线状态复选框
            /*var online_status_html = '<div class="form-group col-xs-12 col-sm-6 col-md-4 col-lg-3">';
            online_status_html += '<label class="control-label col-xs-4">在线状态</label>';
            online_status_html += '<div class="col-xs-8">';
            online_status_html += '<select class="form-control" name="online_status">';
            online_status_html += '<option value="">全部</option>';
            online_status_html += '<option value="1">在线</option>';
            // online_status_html += '<option value="0">离线</option>';
            online_status_html += '</select>';
            online_status_html += '</div>';
            online_status_html += '</div>';
            $('.form-commonsearch >fieldset >.row').children().last().before(online_status_html);*/

            Form.events.cxselect(form);
            Form.events.selectpage(form);
          });

          // 初始化表格
          table.bootstrapTable({
            url: $.fn.bootstrapTable.defaults.extend.index_url,
            pk: 'id',
            sortName: 'id',
            fixedColumns: true,
            fixedRightNumber: 1,
            columns: [
              [
                // {checkbox: true},
                {field: 'id', title: __('Id')},
                {field: 'zone_id', title: __('Zone_id'), visible: false,},
                {
                  field: 'zone.name', title: __('Zone.name'), operate: false, formatter: function (value, row, index) {
                    return value + '(' + row.zone_id + ')';
                  }
                },
                {field: 'user_id', title: __('User_id'), visible: false},
                {
                  field: 'user.nickname', title: __('User.nickname'), operate: false, formatter: function (value, row, index) {
                    return value + '(' + row.user_id + ')';
                  }
                },

                // {field: 'name', title: __('Name'), operate: 'LIKE'},
                {field: 'real_name', title: __('Real_name'), operate: 'LIKE'},
                {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                {field: 'gender', title: __('Gender'), searchList: {"1":__('Gender 1'),"0":__('Gender 0')}, formatter: Table.api.formatter.normal},
                // {field: 'id_card_no', title: __('Id_card_no'), operate: 'LIKE'},
                {field: 'lng', title: __('Lng'), operate: false},
                {field: 'lat', title: __('Lat'), operate: false},
                // {field: 'online_status', title: __('online_status'), searchList: {"1":__('online_status 1'),"0":__('online_status 0')}, formatter: Table.api.formatter.status},
                {field: 'work_status', title: __('Work_status'), searchList: {"1":__('Work_status 1'),"0":__('Work_status 0')}, formatter: Table.api.formatter.status},
                {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"0":__('Status 0')}, formatter: Table.api.formatter.status},
                {
                  field: 'day_order_limit', title: __('Day_order_limit'), operate: false, formatter: function (value, row, index) {
                    if (value == 0) {
                      return '不限';
                    }
                    return value + '单';
                  }
                },
                {
                  field: 'taking_order_limit', title: __('Taking_order_limit'), operate: false, formatter: function (value, row, index) {
                    if (value == 0) {
                      return '不限';
                    }
                    return value + '单';
                  }
                },
                {
                  field: 'today_order_count', title: __('Today_order_count'), operate: false, formatter: function (value, row, index) {
                    if (value == 0) {
                      return '不限';
                    }
                    return value + '单';
                  }
                },
                {
                  field: 'taking_order_count', title: __('Taking_order_count'), operate: false, formatter: function (value, row, index) {
                    if (value == 0) {
                      return '不限';
                    }
                    return value + '单';
                  }
                },
                {
                  field: 'zone_delivery_fee_rate', title: __('Zone_delivery_fee_rate'), operate: false,
                  formatter: function (value, row, index) {
                    return (value * 100) + '%';
                  }
                },
                {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                {
                  field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                  buttons: [
                    {
                      name: 'choose',
                      text: '选择',
                      title: '选择',
                      classname: 'btn btn-xs btn-info btn-click',
                      icon: 'fa fa-check-circle',
                      click: function (index, row) {
                        console.log('choose row', row);
                        // 回传数据给Fast.api.open的callback,很重要
                        Fast.api.close({rider:row});
                      }
                    }
                  ],
                }
              ]
            ]
          });

          // 为表格绑定事件
          Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'tongcheng/rider/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'tongcheng/rider/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'tongcheng/rider/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            // 选择骑手
            $(document).on("click", ".btn-choose", function () {

            });
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        multiset: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
